﻿using System.Text;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.DataAccess.Engine
{
    public class MSSQLFilterParser : FilterParser
    {
        public override string ToSQL()
        {
            var sql = string.Empty;
            var cond = Filter.Condition.ToLower();
            if (NormalOpe.Contains(cond))
            {
                sql = base.ToSQL();
            }
            else
            {
                if (IsNULLValue(out string nullSQL))
                {
                    return nullSQL;
                }
                switch (cond)
                {
                    case QueryOperator.Contains:
                        sql = $"({Filter.Code} LIKE Concat('%',{ValueCode},'%') )";
                        break;
                    case QueryOperator.NotContains:
                        sql = $"({Filter.Code} NOT LIKE Concat('%',{ValueCode},'%') )";
                        break;
                    case QueryOperator.FixedLengthContains:
                        sql = $"({Filter.Code} LIKE {ValueCode})";
                        break;
                    case QueryOperator.StartsWith:
                        sql = $"({Filter.Code} LIKE Concat({ValueCode},'%') )";
                        break;
                    case QueryOperator.EndsWith:
                        sql = $"({Filter.Code} LIKE Concat('%',{ValueCode}) )";
                        break;
                    case QueryOperator.In:
                        StringBuilder sbIn = new StringBuilder();
                        for (int i = 0; i < Filter.Value.Count; i++)
                        {
                            sbIn.Append(ValueCode + "_" + "IN_" + i);
                            if (i < Filter.Value.Count - 1)
                                sbIn.Append(",");
                        }
                        sql = $"({Filter.Code} IN ({sbIn}))";
                        break;
                    case QueryOperator.NotIn:
                        StringBuilder sbNotIn = new StringBuilder();
                        for (int i = 0; i < Filter.Value.Count; i++)
                        {
                            sbNotIn.Append(ValueCode + "_" + "NOTIN_" + i);
                            if (i < Filter.Value.Count - 1)
                                sbNotIn.Append(",");
                        }
                        sql = $"({Filter.Code} NOT IN ({sbNotIn}))";
                        break;
                    case QueryOperator.Between:
                        var value = Filter.Value;
                        if (value.Count == 1)
                            sql = $"({Filter.Code} {QueryOperator.GreaterEqualThan} {ValueCode})";
                        else if (value.Count == 2)
                            sql = $"({Filter.Code} {QueryOperator.GreaterEqualThan} {ValueCode + "_Prev"} and {Filter.Code} {QueryOperator.LessEqualThan} {ValueCode + "_Next"})";
                        break;
                    default:
                        break;
                }
            }
            return sql;
        }
    }
}
